$(document).ready(function(){
    var pages = $("#pages").val();
    var $update = $('#page');

    search();

    //Funcion del paginador
    $("#slider").pagination( {
        total : pages,
        onChange : function( value ) {
            $update.val(value);
            search();
            $("html, body").animate({ scrollTop: 0 }, "slow");
        }
    });
    
    $("#newUser").on('click', function(e){
        e.preventDefault();
        var $form = $('form').serialize();
        $.ajax({
            url : URL()+'/users/manage_users_new',
            type : 'GET',
            data : $form,
            success : function(data){
                setNewToken();
                $.colorbox({
                    html:data.html,
                    innerWidth: 992,
                    innerHeight: false,
                    maxWidth: "95%",
                    maxHeight: "95%"
                });
            }
        });
    });
    
    $('body').on('submit', '#formUser', function(e){
        //este es cuando guarda
        e.preventDefault();
        var $id = $('#id').val() || '';
        var $form = $(this);
        var $data = $form.serialize();
        var $action = $form.attr( "action" );
        $.ajax({
            url : $action+'/'+$id,
            type : 'POST',
            data : $data,
            success : function(data){
                setNewToken();
                if(data.response === 2){
                    manager_user.saveOk();
                } else if (data.response === 1){
                    $('#cboxLoadedContent').html(data.html);
                }
            }
        });
    });

    $('body').on('click', 'a.update', function(e){
        e.preventDefault();
        var $id =  $(this).parents('tr').attr( "u_id" );
        var $form = $('form').serialize();
        $.ajax({
            url : URL()+'/users/manage_users_update/'+$id,
            type : 'GET',
            data : $form,
            success : function(data){
                setNewToken();
                $.colorbox({
                    html:data.html,
                    innerWidth: 992,
                    innerHeight: false,
                    maxWidth: "95%",
                    maxHeight: "95%"
                });
            }
        });
    });
    
    $('body').on('click', 'a.delite', function(e){
        e.preventDefault();
        var $fila = $(this).parents('tr');
        var $id =  $fila.attr( "u_id" );
        var $form = $('form').serializeArray();
        $form.push({name:'id',value:$id});
//    var msg = "¿Desea dejar eliminar el usuarios?";
//    bootbox.confirm( msg, function(result) {
//    
//    });        
        $.ajax({
            url : URL()+'/users/manage_users_delete',
            type : 'POST',
            data : $form,
            success : function(data){
                setNewToken();
                if(data.response){
                    $fila.remove();
                }
            }
        });
    });    
    
});


//Funcion que genera la tabla
function search(){
    var $form = $('form').serialize();
    $.ajax({
        url : URL()+'/users/manage_users_table',
        data : $form,
        type : 'POST',
        success : function(resultado){
            setNewToken();
            $('#table').html(resultado);
        }
    });
}

var manager_user = {
  saveOk: function(){
      $('#cboxLoadedContent').html('Guardado ok');
      setTimeout(function(){ $(window).colorbox.close(); }, 700);
      search();
  }
    
};